import java.io.BufferedReader;  
import java.io.BufferedWriter;  
import java.io.File;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;
import java.io.FileReader;  
import java.io.FileWriter;  
import java.io.IOException;  
import java.io.OutputStreamWriter;

  
public class FormatDoc {  
    public static int j = 1;  
  
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
    	String path;
    	path = FormatDoc.class.getResource("").toString();
    	//去除文件前缀,取得文件路径
    	path = path.substring(6, path.length());
    	System.out.println(path);
        File file = new File(path);  
        searchDirectory(file, 0);  
        System.out.println("OVER");  
    }  
  
    public static void searchDirectory(File f, int depth) {  
        if (!f.isDirectory()) {  
            String fileName = f.getName();  
            if (fileName.matches(".*.{1}java")) {  
                annotation(f);  
            }  
        } else {  
            File[] fs = f.listFiles();  
            depth++;  
            for (int i = 0; i < fs.length; ++i) {  
                File file = fs[i];  
                searchDirectory(file, depth);  
            }  
        }  
    }  
  
    public static void annotation(File f) {  
        String content = FormatDoc.read(f);  
        FormatDoc.write(content, f);  
        System.out.println(j++);  
        return;  
  
    }  
  
    public static String read(File src) {  
        StringBuffer res = new StringBuffer();  
        String line = null;  
        try {  
            BufferedReader reader = new BufferedReader(new FileReader(src));  
            int i = 0;  
            while ((line = reader.readLine()) != null) {  
                if (i != 0) {  
                    res.append('\n');  
                }  
                res.append(line);  
                i++;  
            }  
            reader.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return res.toString();  
    }  
  
    public static boolean write(String cont, File dist) {  
        try {  
        	OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(dist),"UTF-8");
//            BufferedWriter writer = new BufferedWriter(new FileWriter(dist));  
            writer.write(cont);  
            writer.flush();  
            writer.close();  
            return true;  
        } catch (IOException e) {  
            e.printStackTrace();  
            return false;  
        }  
    }  
}  